# coding=utf-8

from mysql import UsingMysql
import re
import codecs

class Orphandrug_Model:
    '''
    药智网-孤儿药与罕见病
    orphandrug Model
    @author spring
    @date 2022.01.21
    '''

    # 序号
    id = ''

    # 数据来源
    data_from = ''

    # 通用名称
    common_name = ''

    # 活性成分(中文)
    active_component = ''

    # 罕见病名
    orphan_name = 0

    # 见病名（药智）
    yzorphan_name= 0

    # 认定时间
    confirm_date = 0

    # 认定状态
    confirm_statues = 0

    # 认定状态（药智）
    yzconfirm_statues = 0

    # 企业名称
    company_name = 0

    # 企业地址
    company_address = 0

    # 隶属国家
    country = 0

    # 靶点
    target = 0

    #表名
    __table_name = 'ai_spider_yaozh_orphandrug'

    def __init__(self, id, data_from, common_name, active_component, orphan_name, yzorphan_name, confirm_date, confirm_statues, yzconfirm_statues, company_name, company_address, country, target):

        """
        初始化
        """
        self.id  = id
        self.data_from  = data_from
        self.common_name  = common_name
        self.active_component  = active_component
        self.orphan_name  = orphan_name
        self.yzorphan_name  = yzorphan_name
        self.confirm_date  = confirm_date
        self.confirm_statues  = confirm_statues
        self.yzconfirm_statues  = yzconfirm_statues
        self.company_name  = company_name
        self.company_address  = company_address
        self.country  = country
        self.target  = target

    #def __str__(self) -> str:
        #return self.serial_id + ',' + self.pharmacy_name + ',' + self.pilot_area

    def save(self):
        """
        保存
        """
        data = self.get(self.id)
        if data:
            self.update()
        else:
            self.insert()    

    def insert(self):
        """
        新增文章
        """
        with UsingMysql(log_time=True) as um:

            sql = (
                "INSERT INTO `"+self.__table_name +"` (`id`,`data_from`,`common_name`,`active_component`,`orphan_name`,`yzorphan_name`,`confirm_date`,`confirm_statues`,`yzconfirm_statues`,`company_name`,`company_address`,`country`,`target`) "
                "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
            )

            sql = sql%(um._conn.escape(self.id),
            um._conn.escape(self.data_from),
            um._conn.escape(self.common_name),
            um._conn.escape(self.active_component),
            um._conn.escape(self.orphan_name),
            um._conn.escape(self.yzorphan_name),
            um._conn.escape(self.confirm_date),
            um._conn.escape(self.confirm_statues),
            um._conn.escape(self.yzconfirm_statues),
            um._conn.escape(self.company_name),
            um._conn.escape(self.company_address),
            um._conn.escape(self.country),
            um._conn.escape(self.target))
            #print(sql)

            um.cursor.execute(sql)
                                
            insert_id = um._conn.insert_id()

            return insert_id

    def get(self, id):
        """
        获取数据
        """
        with UsingMysql(log_time=True) as um:
            um.cursor.execute("SELECT * from `"+self.__table_name+"` WHERE `id`='"+id+"'")
            return um.cursor.fetchone()

    def update(self):
        '''
        编辑数据
        '''
        with UsingMysql(log_time=True) as um:

            sql = (
                "UPDATE `"+self.__table_name +"` SET `data_from`=%s,`common_name`=%s,`active_component`=%s,`orphan_name`=%s,`yzorphan_name`=%s,`confirm_date`=%s,`confirm_statues`=%s,`yzconfirm_statues`=%s,`company_name`=%s,`company_address`=%s,`country`=%s,`target`=%s"
                " WHERE `serial_id`=%s"
            )

            um.cursor.execute(sql)
                                
            return um._conn.affected_rows
